/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.locationbasedagent;


import android.content.Context;
import android.content.res.TypedArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {
    int mGalleryItemBackground;
    private Context mContext;
//    private static final String[] URLS = {
//    	"http://i836.photobucket.com/albums/zz289/olras11/P1000080.jpg",
//        "http://i836.photobucket.com/albums/zz289/olras11/P1000073-1.jpg",
//        "http://i836.photobucket.com/albums/zz289/olras11/P1000074.jpg",
//        "http://i836.photobucket.com/albums/zz289/olras11/P1000075.jpg",
//        "http://i836.photobucket.com/albums/zz289/olras11/P1000079.jpg",
//        "http://i836.photobucket.com/albums/zz289/olras11/P1000081.jpg",
//        "http://i836.photobucket.com/albums/zz289/olras11/P1000082.jpg"
//    };
    private String[] URLS;
    
    public ImageAdapter(Context c) {
        mContext = c;
        TypedArray attr = mContext.obtainStyledAttributes(R.styleable.Lab2Assignment);
        mGalleryItemBackground = attr.getResourceId(
                R.styleable.Lab2Assignment_android_galleryItemBackground, 0);
        attr.recycle();
    }
    
    private final ImageDownloader imageDownloader = new ImageDownloader();
    
    public int getCount() {
        return URLS.length;
    }

    public String getItem(int position) {
        return URLS[position];
    }

    public long getItemId(int position) {
        return URLS[position].hashCode();
    }
    
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = new ImageView(mContext);

        //imageView.setImageResource(new Uri(URLS[position]));
        imageView.setLayoutParams(new Gallery.LayoutParams(150, 100));
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setBackgroundResource(mGalleryItemBackground);

        imageDownloader.download(URLS[position], imageView);
        
        return imageView;
    }


    public ImageDownloader getImageDownloader() {
        return imageDownloader;
    }
    
    public void setImageURLs(String[] urls) {
    	URLS = urls;
    }
}
